
package com.sdhs.paas.authclient.jwt;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.sdhs.paas.authclient.config.ServiceAuthConfig;
import com.sdhs.paas.authclient.service.ServiceAuthFeign;
import com.sdhs.paas.authshare.common.msg.BaseResponse;
import com.sdhs.paas.authshare.common.msg.ObjectRestResponse;
import com.sdhs.paas.authshare.exception.auth.ClientTokenException;
import com.sdhs.paas.authshare.util.jwt.IJWTInfo;
import com.sdhs.paas.authshare.util.jwt.JWTHelper;

import dto.ClientDTO;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;

/**
 * Created by ace on 2017/9/15.
 */
@Configuration
@EnableScheduling
public class ServiceAuthUtil{
    @Autowired
    private ServiceAuthConfig serviceAuthConfig;
    
//    @Autowired
//    private AuthService authService;
    @Autowired
    private ServiceAuthFeign serviceAuthFeign;

    private List<String> allowedClient;
    private String clientToken;


    public IJWTInfo getInfoFromToken(String token) throws Exception {
        try {
            return JWTHelper.getInfoFromToken(token, serviceAuthConfig.getPubKeyByte());
        } catch (ExpiredJwtException ex) {
            throw new ClientTokenException("Client token expired!");
        } catch (SignatureException ex) {
            throw new ClientTokenException("Client token signature error!");
        } catch (IllegalArgumentException ex) {
            throw new ClientTokenException("Client token is null or empty!");
        }
    }

    @Scheduled(cron = "0/30 * * * * ?")
    public void refreshAllowedClient() {
//        log.debug("refresh allowedClient.....");
//    	BaseResponse resp = new BaseResponse();
//    	List<String> clientList = authService.getAllowedClient(serviceAuthConfig.getClientId(), serviceAuthConfig.getClientSecret());
//    	this.allowedClient=clientList;
    	ClientDTO clientDTO =new ClientDTO();
    	clientDTO.setServiceId(serviceAuthConfig.getClientId());
    	clientDTO.setSecret(serviceAuthConfig.getClientSecret());
        BaseResponse resp = serviceAuthFeign.getAllowedClient(clientDTO);
        if ("000000".equals(resp.getCode())) {
            ObjectRestResponse<ClientDTO> allowedClient = (ObjectRestResponse<ClientDTO>) resp;
            this.allowedClient = allowedClient.getData().getAllowClientList();
        }
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void refreshClientToken() {
//    	this.clientToken = authService.getAccessToken(serviceAuthConfig.getClientId(), serviceAuthConfig.getClientSecret());
//        log.debug("refresh client token.....");
//        BaseResponse resp = new BaseResponse();
    	ClientDTO clientDTO =new ClientDTO();
    	clientDTO.setClientId(serviceAuthConfig.getClientId());
    	clientDTO.setSecret(serviceAuthConfig.getClientSecret());
    	BaseResponse resp = serviceAuthFeign.getAccessToken(clientDTO);
        if ("000000".equals(resp.getCode())) {
            ObjectRestResponse<ClientDTO> clientToken = (ObjectRestResponse<ClientDTO>) resp;
            this.clientToken = clientToken.getData().getToken();
        }
    }


    public String getClientToken() {
        if (this.clientToken == null) {
            this.refreshClientToken();
        }
        return clientToken;
    }

    public List<String> getAllowedClient() {
        if (this.allowedClient == null) {
            this.refreshAllowedClient();
        }
        return allowedClient;
    }
}